package entity

// 本文件提供ConditionRule和ConditionGroup的使用示例
// 展示如何配置复杂的条件逻辑来满足图片中显示的需求
// 注意：已从旧的ConditionGroups结构迁移到新的ConditionRules结构

// ExampleSimpleConditionGroup 简单条件组示例
// 演示单个条件组的基本用法
func ExampleSimpleConditionGroup() NodeTemplate {
	// 创建条件节点
	node := NewNodeTemplate(
		"simple-condition",
		"简单条件判断",
		"", // 条件节点不需要默认的NextNodeTemplateID
		NodeTemplateKindCondition,
		"workflow-001",
	)

	// 配置条件规则：当金额大于等于10000时跳转到总监审批
	node.ConditionRules = []ConditionRule{
		{
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "amount",
							Operator: ">=", // 大于等于
							Value:    10000,
						},
					},
					LogicalOperator: LogicalOperatorAnd, // 单个条件时AND/OR无区别
				},
			},
			NextNodeTemplateID: "director-approval",
		},
		// 默认分支：其他情况跳转到经理审批
		{
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "amount",
							Operator: "<", // 小于
							Value:    10000,
						},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "manager-approval",
		},
	}

	return node
}

// ExampleComplexConditionGroup 复杂条件组示例
// 演示多条件AND逻辑的使用，对应图片中的需求
func ExampleComplexConditionGroup() NodeTemplate {
	// 创建条件节点
	node := NewNodeTemplate(
		"complex-condition",
		"复杂条件判断",
		"",
		NodeTemplateKindCondition,
		"workflow-002",
	)

	// 配置复杂条件规则，模拟图片中的需求
	node.ConditionRules = []ConditionRule{
		{
			// 条件1：业务字段=审批环节2 AND 文件类型=9 AND 借用天数大于3
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "businessField",
							Operator: "==", // 等于
							Value:    "审批环节2",
						},
						{
							Param:    "fileType",
							Operator: "==", // 等于
							Value:    9,
						},
						{
							Param:    "borrowDays",
							Operator: ">", // 大于
							Value:    3,
						},
					},
					LogicalOperator: LogicalOperatorAnd, // 所有条件都必须满足
				},
			},
			NextNodeTemplateID: "special-approval-1",
		},
		{
			// 条件2：业务字段=审批环节2 AND 文件类型=10
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "businessField",
							Operator: "==", // 等于
							Value:    "审批环节2",
						},
						{
							Param:    "fileType",
							Operator: "==", // 等于
							Value:    10,
						},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "special-approval-2",
		},
		// 默认分支
		{
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "businessField",
							Operator: "!=", // 不等于
							Value:    nil, // 任何非空值都会满足这个条件
						},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "default-approval",
		},
	}

	return node
}

// ExampleOrLogicConditionGroup OR逻辑条件组示例
// 演示多条件OR逻辑的使用
func ExampleOrLogicConditionGroup() NodeTemplate {
	// 创建条件节点
	node := NewNodeTemplate(
		"or-logic-condition",
		"OR逻辑条件判断",
		"",
		NodeTemplateKindCondition,
		"workflow-003",
	)

	// 配置OR逻辑条件规则
	node.ConditionRules = []ConditionRule{
		{
			// 紧急情况：金额超过100万 OR 标记为紧急 OR VIP客户
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "amount",
							Operator: ">", // 大于
							Value:    1000000,
						},
						{
							Param:    "urgent",
							Operator: "==", // 等于
							Value:    true,
						},
						{
							Param:    "customerLevel",
							Operator: "==", // 等于
							Value:    "VIP",
						},
					},
					LogicalOperator: LogicalOperatorOr, // 任意一个条件满足即可
				},
			},
			NextNodeTemplateID: "urgent-approval",
		},
		// 普通情况
		{
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "amount",
							Operator: "<=", // 小于等于
							Value:    1000000,
						},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "normal-approval",
		},
	}

	return node
}

// ExampleStringConditionGroup 字符串条件组示例
// 演示字符串包含操作符的使用
func ExampleStringConditionGroup() NodeTemplate {
	// 创建条件节点
	node := NewNodeTemplate(
		"string-condition",
		"字符串条件判断",
		"",
		NodeTemplateKindCondition,
		"workflow-004",
	)

	// 配置字符串条件规则
	node.ConditionRules = []ConditionRule{
		{
			// 特殊部门处理：部门名称包含"财务"或"人事"
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "department",
							Operator: "⊇", // 包含
							Value:    "财务",
						},
						{
							Param:    "department",
							Operator: "⊇", // 包含
							Value:    "人事",
						},
					},
					LogicalOperator: LogicalOperatorOr, // 包含任意一个关键词即可
				},
			},
			NextNodeTemplateID: "special-dept-approval",
		},
		{
			// 排除测试环境：环境名称不包含"test"
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "environment",
							Operator: "!⊇", // 不包含
							Value:    "test",
						},
						{
							Param:    "environment",
							Operator: "!⊇", // 不包含
							Value:    "dev",
						},
					},
					LogicalOperator: LogicalOperatorAnd, // 都不能包含
				},
			},
			NextNodeTemplateID: "production-approval",
		},
		// 默认分支
		{
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{
							Param:    "department",
							Operator: "!=", // 不等于
							Value:    nil,
						},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "default-approval",
		},
	}

	return node
}

// ExampleUsageInWorkflow 在工作流中的使用示例
// 演示如何在实际业务中使用条件组
func ExampleUsageInWorkflow() {
	// 1. 创建条件节点
	conditionNode := ExampleComplexConditionGroup()

	// 2. 准备条件参数（通常来自业务数据）
	conditionParams := ConditionParams{
		"businessField": "审批环节2",
		"fileType":      9,
		"borrowDays":    5,
		"amount":        15000,
		"department":    "技术部",
	}

	// 3. 评估条件并获取下一个节点ID
	nextNodeID := conditionNode.GetNextApprovalNodeTemplateID(conditionParams)

	// 4. 根据结果进行后续处理
	switch nextNodeID {
	case "special-approval-1":
		// 跳转到特殊审批流程1
		// 处理业务逻辑...
	case "special-approval-2":
		// 跳转到特殊审批流程2
		// 处理业务逻辑...
	case "default-approval":
		// 跳转到默认审批流程
		// 处理业务逻辑...
	default:
		// 没有匹配的条件，可能是配置错误
		// 记录错误日志并处理异常情况...
	}
}

// ExampleMigrationFromLegacy 从旧版本迁移的示例
// 演示如何将旧的expression结构迁移到新的ConditionGroup结构
// ExampleConditionGroupsOrRelationship 演示条件组间OR关系的示例
// 展示多个条件组如何通过OR逻辑进行组合
func ExampleConditionGroupsOrRelationship() NodeTemplate {
	// 创建条件节点
	node := NewNodeTemplate(
		"or-relationship-example",
		"条件组OR关系示例",
		"",
		NodeTemplateKindCondition,
		"workflow-005",
	)

	// 配置多个条件规则，它们之间为OR关系
	node.ConditionRules = []ConditionRule{
		{
			// 条件组1：财务部门的申请
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{Param: "department", Operator: "==", Value: "finance"},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "finance-approval",
		},
		{
			// 条件组2：高优先级申请
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{Param: "priority", Operator: "==", Value: "high"},
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "high-priority-approval",
		},
		{
			// 条件组3：小额申请（自动审批）
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{Param: "amount", Operator: "<", Value: 1000}, // 小于1000
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "auto-approval",
		},
		{
			// 默认条件组：其他情况
			ConditionGroups: []ConditionGroup{
				{
					Conditions: []expression{
						{Param: "amount", Operator: ">=", Value: 0}, // 大于等于0（兜底条件）
					},
					LogicalOperator: LogicalOperatorAnd,
				},
			},
			NextNodeTemplateID: "default-approval",
		},
	}

	// 评估逻辑：
	// 1. 如果department=finance，跳转到finance-approval
	// 2. 否则如果priority=high，跳转到high-priority-approval  
	// 3. 否则如果amount<1000，跳转到auto-approval
	// 4. 否则跳转到default-approval

	return node
}